/** @author A0105514U */
// @author A0105514U
package storage;

import static org.junit.Assert.*;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import global.Task;
import global.Time;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class StorageUnitTestDriver {
	Task task;
	String str1,str2,str3,str4;
	String modRecordFolder,path;
	
	@Before
	public void setUp(){
		Storage.init();
		try {
			
			String userName = System.getProperty("user.name");
			path = "C:\\Users\\"+userName+"\\Documents\\BeautyRoxy Storage";
			modRecordFolder = "C:\\Users\\"+userName+"\\Documents\\BeautyRoxy Storage\\Modification Record";
			
			task = new Task("time","homework","",new Time("201310110101"), new Time("201310111111"));
			Storage.addFile(task);
			str1 = task.getTaskFileName();
			
			task = new Task("float","read Harry Porter");
			Storage.addFileWithoutRecord(task);
			str2 = task.getTaskFileName();
			
			task = new Task("time","CS2103 Demo","",new Time("201310110101"), new Time("201310111111"));
			Storage.addFile(task);
			str3 = task.getTaskFileName();
			
			task = new Task("deadline","CS2106 project","",new Time("201311152359"));
			Storage.addFile(task);
			str4 = task.getTaskFileName();
			
			Storage.storeRecord();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	// Testing modRecord
	@Test
	public void test1(){
		// to retrieve the record
		Storage.init();
		assertEquals(Storage.getRecord().toString(),"add#201310111111homework1.txt#");
		assertEquals(Storage.getRecord().toString(),"add#201310111111CS2103 Demo1.txt#");
		assertEquals(Storage.getRecord().toString(),"add#201311152359CS2106 project2.txt#");
	}
	
	// Testing file existence
	@Test
	public void test2(){
		randomOperations();
		
		File file1 = new File(path+"\\"+str1);
		File file2 = new File(path+"\\"+str2);
		File file3 = new File(path+"\\"+str3);
		File file4 = new File(path+"\\"+str4);
		
		assertTrue(file1.exists());
		assertFalse(file2.exists());
		assertTrue(file3.exists());
		assertTrue(file4.exists());
	}
	
	private void randomOperations(){
		try{
			Storage.deleteFileWithoutRecord(str2);
			Storage.deleteFileWithoutRecord(str4);
			Storage.addFileWithoutRecord(task);
		} catch (IOException e) {
			assertTrue(false);
			e.printStackTrace();
		}
	}
	
	@After
	public void cleanUp(){
		Storage.deleteFileWithoutRecord(str1);
		Storage.deleteFileWithoutRecord(str2);
		Storage.deleteFileWithoutRecord(str3);
		Storage.deleteFileWithoutRecord(str4);
		
		String modRecordFile = modRecordFolder+"\\Command Storage.txt";
		File file = new File(modRecordFile);
		if(file.exists()){
			file.delete();
		}
	}
}